﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class EditVendorInvoice : System.Web.UI.Page
    {
        Helper helper = new Helper();
        VendorInvoiceController vendorInvoiceController=new VendorInvoiceController();
        VendorPOController vendorPOController=new VendorPOController();
        VendorPOEntity vendorPOEntity = new VendorPOEntity();
        VendorController vendorController = new VendorController();
        VendorPOItemController vendorPOItemController = new VendorPOItemController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["vendorInvoiceId"]!=null)
            {
                if(!IsPostBack)
                {
                    int vendorInvoiceId = Convert.ToInt32(Request.QueryString["vendorInvoiceId"]);
                    IList<VendorPOEntity> vendorPOEntities = vendorPOController.findVendorPOByStatus("new");
                    PONoComboBox.DataSource = vendorPOEntities;
                    PONoComboBox.DataTextField = "No";
                    PONoComboBox.DataValueField = "Id";
                    PONoComboBox.DataBind();
                    int vendorPOId = Convert.ToInt32(PONoComboBox.SelectedValue);
                    VendorPOController vendorPoController = new VendorPOController();
                    vendorPOEntity = vendorPoController.findVendorPOById(vendorPOId);
                    int vendorId = vendorPOEntity.VendId;
                    VendorEntity vendorEntity = vendorController.findVendorByVendorId(vendorId);
                    string vendorName = vendorEntity.Name;
                    VendorLabel.Text = vendorName;
                    IList<VendorPOItemEntity> vendorPOItemEntities = vendorPOItemController.findVendorPOItemsByVendorPOId(vendorPOId);
                    POItemAndRequestedDateDropDownList.DataSource = vendorPOItemEntities;
                    POItemAndRequestedDateDropDownList.DataTextField = "ItemAndRequestedDate";
                    POItemAndRequestedDateDropDownList.DataValueField = "Id";
                    POItemAndRequestedDateDropDownList.DataBind();
                    POItemAndRequestedDateDropDownList.Items.Insert(0, new ListItem("--Select Item--", "0"));
                    SuccessfulLabel.Text = "";

                    if (POItemAndRequestedDateDropDownList.SelectedIndex == 0)
                    {
                        InsertButton.Enabled = false;
                    }

                    DateTextBox_CalendarExtender.SelectedDate = System.DateTime.Now;

                    VendorInvoiceEntity vendorInvoiceEntity =
                        vendorInvoiceController.FindVendorInvoiceById(vendorInvoiceId);
                    InvoiceNoTextBox.Text = vendorInvoiceEntity.VendorInvoiceNo;

                    IList<StorageEntity> storageEntities =
                        vendorInvoiceController.FindVendorInvoiceStorages(vendorInvoiceId);
                    InvoiceItemGridView.DataSource = storageEntities;
                    InvoiceItemGridView.DataBind();
                    Session["VendorInvoiceItems"] = storageEntities;
                }
            }
            else
            {
                Session["FlashMessage"] = "Error: You Have Not Seleted A Correct Vendor Invoice!";
                Session["FlashMessageType"] = "error";
                Response.Redirect("VendorInvoiceList.aspx");
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void POItemAndRequestedDateDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void InsertButton_Click(object sender, EventArgs e)
        {

        }

        protected void UpdateVendorInvoiceButton_Click(object sender, EventArgs e)
        {

        }

        protected void PONoComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void SearchPONoButton_Click(object sender, EventArgs e)
        {

        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                int rowIndex = ((GridViewRow) ((Button) sender).NamingContainer).RowIndex;
                IList<StorageEntity> storageEntities = Session["VendorInvoiceItems"] as IList<StorageEntity>;
                storageEntities.RemoveAt(rowIndex);

                InvoiceItemGridView.DataSource = storageEntities;
                InvoiceItemGridView.DataBind();
            }
            catch(Exception exception){}
        }
    }
}